﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using System.Text;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model.Calendar;
using System.Reflection;
using Cn.Cellula.Client;
using Cn.Cellula.Biz.Def;
using Cn.Edwon.Util;
namespace Cn.Cellula.Website.Admin.Calendar {
    public partial class Default : PageSupport {
        DateTime CurDt = DateTime.Now;
        ICalendarBiz biz = BizFactory.Get<ICalendarBiz>();
        public String TodoData = "";
        private Int32 Mode = 0;//0:week，1:month
        protected void Page_Load(object sender, EventArgs e) {
            try {
                Mode = Convert.ToInt32(Request.QueryString["mode"]);
            } catch (Exception ex) {
                Mode = 0;
                log.Error("Calendar:Mode Param Exception:" + ex.Message);
            }
            if (!String.IsNullOrEmpty(Request.QueryString["dt"])) {
                try {
                    CurDt = Convert.ToDateTime(Request.QueryString["dt"]);
                } catch { }
            }
            if (!Page.IsPostBack) {
                if (Mode == 1) {
                    this.lab_ModeText.Text = "月历";
                    this.hl_Next.Text = "下一月";
                    this.hl_Next.NavigateUrl = "Admin/Calendar/Default.aspx?mode=1&dt=" + CurDt.AddMonths(1).ToString("yyyy-M-d");
                    this.hl_Prev.Text = "上一月";
                    this.hl_Prev.NavigateUrl = "Admin/Calendar/Default.aspx?mode=1&dt=" + CurDt.AddMonths(-1).ToString("yyyy-M-d");
                    DrawCalendarMonthly();
                } else {
                    this.lab_ModeText.Text = "周历";
                    this.hl_Next.Text = "下一周";
                    this.hl_Next.NavigateUrl = "Admin/Calendar/Default.aspx?mode=0&dt=" + CurDt.AddDays(7).ToString("yyyy-M-d");
                    this.hl_Prev.Text = "上一周";
                    this.hl_Prev.NavigateUrl = "Admin/Calendar/Default.aspx?mode=0&dt=" + CurDt.AddDays(-7).ToString("yyyy-M-d");
                    DrawCalendarWeekly();
                }
            }
        }

        /// <summary>
        /// 按自然周显示
        /// </summary>
        private void DrawCalendarWeekly() {
            DateTime first = DateTime.Now;
            DateTime last = DateTime.Now;
            this.lab_title.Text = CurDt.Year + "年，第" + DateUtil.WeekOfYear(CurDt) + "周";
            this.lab_Calendar.Text = ClientBiz.BuildCalendarHtmlCode(CurDt, 0, ref first, ref last);
            LoadTodo(first, last);
        }
        private void DrawCalendarMonthly() {
            DateTime first = DateTime.Now;
            DateTime last = DateTime.Now;
            this.lab_title.Text = CurDt.ToString("yyyy年MM月");
            this.lab_Calendar.Text = ClientBiz.BuildCalendarHtmlCode(CurDt, 1, ref first, ref last);
            LoadTodo(first, last);
        }

        private void LoadTodo(DateTime rFirst, DateTime rLast) {
            IList<VCalendarTodoEO> list = biz.LoadCalendarTodo(this.OwnerKey, this.Actor.ID, rFirst, rLast, TodoStatus.All);
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            if (list != null && list.Count > 0) {
                for (int i = 0; i < list.Count; i++) {
                    VCalendarTodoEO eo = list[i];
                    if (i > 0)
                        sb.Append(",");
                    //todokey
                    sb.Append("[\"" + eo.TodoKey + "\"");
                    //start to plan
                    if (eo.StartToPlan == null) {
                        sb.Append(",\"--:--\"");
                    } else {
                        sb.Append(",\"" + Convert.ToDateTime(eo.StartToPlan).ToString("HH:mm") + "\"");
                    }
                    //if Assigned
                    if (eo.AssignToUser != eo.Operator) {
                        if (eo.AssignToUser == this.Actor.ID) {//指派当前用户的 
                            sb.Append(",1");
                        } else {//指派给别人的
                            sb.Append(",2");
                        }
                    } else {
                        sb.Append(",0");
                    }
                    //title
                    sb.Append(",\"" + eo.TodoTitle + "\"");
                    //level
                    sb.Append(",\"" + eo.TodoLevel + "\"");
                    //ifclose
                    sb.Append("," + eo.IfClose);
                    //cellKey
                    sb.Append(",\"cell_" + Convert.ToDateTime(eo.OccurDt).ToString("yyyy_MM_dd") + "\"");
                    sb.Append("]");
                }
            }
            sb.Append("]");
            TodoData = sb.ToString();
        }
        protected void btn_EOAdd_Click(object sender, EventArgs e) {
            Response.Redirect("EditTodo.aspx?rd=0&dt=" + CurDt.ToShortDateString());
        }

    }
}